using QAssetBundle;
using QFramework;
using System;
using UnityEngine;
using UnityEngine.UIElements;

namespace ProjectSurvivor
{
    public partial class Enemy : ViewController, IEnemy
    {
        public float Hp = 3;
        public float MoveMentSpeed = 2.0f;
        public Color DissolveColor = Color.yellow;
        public bool TreasureChestEnemy = false;
        private bool isIngure = false;
        public void Hurt(float value,bool isforce =false,bool iscirtial =false)
        {
            if (isIngure) return;
            isIngure = true;
            SelfRigidbody2D.velocity = Vector3.zero;
            Sprite.color = Color.red;
            AudioKit.PlaySound("Hit");
            FloatingTextController.Play(transform.position, value.ToString("0"), iscirtial);
            ActionKit.Delay(0.2f, () =>
            {
                this.Hp -= value;
                this.Sprite.color = Color.white;
                isIngure = false;
            })
                .Start(this);


        }

        void Start()
        {
            // Code Here
            EnemyGenerator.enemyCount.Value++;
        }

        private void Update()
        {


            if (Hp <= 0)
            {

                EnemyGenerator.enemyCount.Value--;
                FXController.Play(Sprite, DissolveColor);
                AudioKit.PlaySound(Sfx.ENEMY_DIE);
                this.DestroyGameObjGracefully();
                Global.GeneratePowerUp(this.gameObject, TreasureChestEnemy);
            }
        }

        private void FixedUpdate()
        {
            if (!isIngure)
            {
                if (Player.Default)
                {
                    var direction = (Player.Default.transform.position - transform.position).normalized;
                    SelfRigidbody2D.velocity = direction * MoveMentSpeed;
                }
                else
                {
                    SelfRigidbody2D.velocity = Vector3.zero;

                }
            }

        }

        public void SetHpScale(float hpScale)
        {
            Hp *= hpScale;
        }

        public void SetSpeedScale(float speedScale)
        {
            MoveMentSpeed *= speedScale;
        }
    }
}
